package com.hjm.java.service;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.util.Log;

import com.hjm.java.entity.GoodsListVO;
import com.hjm.java.entity.Store;
import com.hjm.java.sql.HJMHelper;
import com.hjm.java.utils.HjmFailCallBack;
import com.hjm.java.utils.HjmListCallBack;
import com.hjm.java.utils.HjmObjectCallBack;
import com.hjm.java.utils.HjmSuccessCallBack;
import com.hjm.java.utils.HjmGlideUtil;
import com.hjm.java.utils.HjmSuccessObjectCallBack;
import com.hjm.ui.comment.entity.Comment;

import java.util.List;

public class StoreService extends BaseService {
    private static final String TAG = "StoreService";

    private List<GoodsListVO> goodsListVOList;

    public StoreService(Context context, Handler mHandler) {
        super(context, mHandler);
    }

    public void getStoreAllGoods(int storeId, HjmSuccessCallBack callBack) {
        httpUtils.getList(baseUrl + "/goods/list?store=" + storeId, GoodsListVO.class, (isSuccess, t, message) -> {
            if (isSuccess) {
                Log.d(TAG, "getGoodsList: 获取商品数据成功，开始获取商品图片 " + t.size());
                HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl,
                        t.stream().map(GoodsListVO -> String.valueOf(GoodsListVO.getIcon())).toArray(String[]::new),
                        HJMHelper.getInstance(mContext),
                        (position, bitmap) -> {
                            Log.d(TAG, "getGoodsList: 商品图片获取成功，位置 " + position);
                            t.get(position).setIconBitmap(bitmap);
                        },
                        () -> {
                            Log.d(TAG, "getGoodsList: 全部商品图片获取成功");
                            this.goodsListVOList = t;
                            mHandler.post(() -> {
                                callBack.onSuccess();
                            });
                        });
            } else {
                Log.d(TAG, "getGoodsList: 获取失败 " + message);
            }
        });
    }

    public List<GoodsListVO> getGoodsListVOList() {
        return goodsListVOList;
    }

    public void getStoreById(int storeId, HjmSuccessObjectCallBack<Store> successObjectCallBack, HjmFailCallBack failCallBack) {
        httpUtils.get(baseUrl + "/store/" + storeId, Store.class, (isSuccess, store, msg) -> {
            mHandler.post(() -> {
                if (isSuccess) {
                    successObjectCallBack.onSuccess(store);
                } else {
                    failCallBack.onFail(msg);
                }
            });
        });
    }

    ;

    public void getStoreIcon(Integer icon, HjmObjectCallBack<Bitmap> callBack) {
        HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl, icon.toString(),
                HJMHelper.getInstance(mContext),
                (isSuccess, bitmap, msg) -> {
                    mHandler.post(() -> {
                        if (isSuccess) {
                            callBack.call(true, bitmap, msg);
                        }
                    });

                });
    }

    public void initStoreComment(int storeId, HjmListCallBack<Comment> callBack) {
        httpUtils.getList(baseUrl + "/comment/store?id=" + storeId, Comment.class, (isSuccess, t, message) -> {
            if (isSuccess) {
                HjmGlideUtil.UrlToBitmap(mContext, baseImgUrl,
                        t.stream().map(Comment -> String.valueOf(Comment.getUserPic())).toArray(String[]::new),
                        HJMHelper.getInstance(mContext),
                        (position, bitmap) -> {
                            t.get(position).setUserPicBitmap(bitmap);
                        },
                        () -> {
                            mHandler.post(() -> {
                                callBack.call(true, t, null);
                            });
                        });
            } else {
                mHandler.post(() -> {
                    callBack.call(false, null, message);
                });
            }
        });
    }
}
